import {
  getCategoryDetail
} from '../../services/category/index';
Page({
  data: {
    pageNo: 0,
    pageSize: 10,
    pageLoading: false,
    childList: [],
    pullDownRefreshing: false,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
    goodsListLoadStatus: 0,
  },
  goodListPagination: {
    pageNo: 1,
    pageSize: 10,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow() {
    this.getTabBar().init()
    this.setData({
      childList: [],
    });
    this.loadHomePage()
  },
  init() {
    this.getChildList();
  },
  getChildList() {
    const that = this;
    wx.showLoading({
      title: '数据加载中...',
    });
    getCategoryDetail({
        param: {
          shelveFlag: 1,
        },
        pageNo: that.data.pageNo,
        pageSize: that.data.pageSize,
      })
      .then((res) => {
        wx.hideLoading();
        for (const item of res.data.entities) {
          if (item.picSmall) {
            item.picSmall = item.picSmall.split(',');
          }
        }
        if (res.data.entities.length < this.data.pageSize) {
          that.setData({
            hidden: true,
          });
        }
        that.setData({
          childList: that.data.childList.concat(res.data.entities),
        });
      })
      .catch((err) => {
        wx.hideLoading();
      });
  },
  gotoDetail(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/subpages/pages/goods/details/index?spuId=${id}`,
    });
  },
  scrollToLower: function (e) {
    this.onSearch();
    var {
      hidden
    } = this.data;
    var that = this;
    if (!hidden) {
      setTimeout(function () {
        that.getChildList();
      }, 200);
    }
  },
  scrollToUpper: function (e) {
    wx.showToast({
      title: '触顶了...',
    });
  },
  onSearch() {
    this.setData({
      pageNo: (this.data.pageNo += 1),
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  onReTry() {
    this.loadGoodsList();
  },
  async loadGoodsList(fresh = false) {
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1
    });
    const pageSize = this.goodListPagination.pageSize;
    let pageIndex = this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      const {
        data
      } = await getCategoryDetail({
        "pageNo": pageIndex,
        "pageSize": pageSize,
        param: {
          shelveFlag: 1,
        },
      });
      for (const item of data.entities) {
        if (item.picSmall) {
          item.picSmall = item.picSmall.split(',');
        }
      }
      this.setData({
        childList: fresh ? data.entities : this.data.childList.concat(data.entities),
        goodsListLoadStatus: (pageIndex == 1 && data.entities.length < 10) ? 2 : data.entities.length > 1 ? 0 : 2,
      });

      this.goodListPagination.pageSize = data.pageSize;
      this.goodListPagination.pageNo = data.pageNo;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },
});